import 'package:awak/common/db/db_helper.dart';
import 'package:awak/common/models/comment.dart';

class CommentRepository {
  // 新增评论
  Future<int> addComment(Comment comment) async {
    final db = await DBHelper.database;
    return await db.insert('comment', comment.toMap());
  }

  // 获取指定房间的所有评论
  Future<List<Comment>> getCommentsByRoomId(int roomId) async {
    final db = await DBHelper.database;
    final res = await db.query(
      'comment',
      where: 'roomId = ?',
      whereArgs: [roomId],
      orderBy: 'id DESC',
    );
    return res.map((e) => Comment.fromMap(e)).toList();
  }

  // 获取所有评论
  Future<List<Comment>> getAllComments() async {
    final db = await DBHelper.database;
    final res = await db.query('comment', orderBy: 'id DESC');
    return res.map((e) => Comment.fromMap(e)).toList();
  }

  // 删除评论
  Future<int> deleteComment(int id) async {
    final db = await DBHelper.database;
    return await db.delete('comment', where: 'id = ?', whereArgs: [id]);
  }

  // 更新评论
  Future<int> updateComment(Comment comment) async {
    final db = await DBHelper.database;
    return await db.update('comment', comment.toMap(), where: 'id = ?', whereArgs: [comment.id]);
  }

  // 根据用户ID获取评论
  Future<List<Comment>> getCommentsByUserId(int userId) async {
    final db = await DBHelper.database;
    final res = await db.query(
      'comment',
      where: 'userId = ?',
      whereArgs: [userId],
      orderBy: 'id DESC',
    );
    return res.map((e) => Comment.fromMap(e)).toList();
  }
}
